package com.quasar.hpatient.module.comm_compile;

import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.TextView;

import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;

import java.io.File;
import java.util.HashMap;
import java.util.regex.Pattern;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import okhttp3.RequestBody;

/**
 * description: 完善信息
 * created by kalu on 2018/4/9 11:10
 */
public class CompilePresenter implements BasePresenter {

    private final String[] mPatient = new String[17];

    void editModel(int position, String str) {
        mPatient[position] = str;
    }

    private final void compileInfo(CompileView view) {

        for (int i = 0; i < mPatient.length; i++) {

            final String message = mPatient[i];

            if (i == 0 && TextUtils.isEmpty(message)) {

                view.toast("真实姓名, 信息不全");
                return;
            } else if (i == 1 && TextUtils.isEmpty(message) && message.length() != 11) {
                view.toast("手机, 信息不全");
                return;
            } else if (i == 2) {

                if (TextUtils.isEmpty(message)) {
                    view.toast("身份证号, 信息不全");
                    return;
                } else {
                    final Pattern compile = Pattern.compile("^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$");
                    if (!compile.matcher(message).matches()) {
                        view.toast("身份证号, 格式不正确");
                        return;
                    }
                }
            } else if (i == 3) {

                if (TextUtils.isEmpty(message)) {
                    view.toast("身高, 信息不全");
                    return;
                } else {
                    final int i1 = Integer.parseInt(message);
                    if (i1 < 50) {
                        view.toast("身高, 不能小于50");
                        return;
                    }
                }
            } else if (i == 4) {

                if (TextUtils.isEmpty(message)) {
                    view.toast("体重, 信息不全");
                    return;
                } else {
                    final int i1 = Integer.parseInt(message);
                    if (i1 < 50) {
                        view.toast("体重, 不能小于3");
                        return;
                    }
                }
            } else if (i == 5) {

                if (TextUtils.isEmpty(message)) {
                    view.toast("邮箱, 信息不全");
                    return;
                } else {
                    final Pattern compile = Pattern.compile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
                    if (!compile.matcher(message).matches()) {
                        view.toast("邮箱, 格式不正确");
                        return;
                    }
                }
            } else if (i == 6 && TextUtils.isEmpty(message)) {
                view.toast("主管医生, 信息不全");
                return;
            } else if (i == 7 && TextUtils.isEmpty(message)) {
                view.toast("性别, 信息不全");
                return;
            } else if (i == 8 && TextUtils.isEmpty(message)) {
                view.toast("出生日期, 信息不全");
                return;
            } else if (i == 9 && TextUtils.isEmpty(message)) {
                view.toast("联系地址, 信息不全");
                return;
            } else if (i == 10 && TextUtils.isEmpty(message)) {
                view.toast("供受体, 信息不全");
                return;
            } else if (i == 11 && TextUtils.isEmpty(message)) {
                view.toast("血型, 信息不全");
                return;
            } else if (i == 12 && TextUtils.isEmpty(message)) {
                view.toast("移植类型, 信息不全");
                return;
            } else if (i == 13 && TextUtils.isEmpty(message)) {
                view.toast("器官来源, 信息不全");
                return;
            } else if (i == 14 && TextUtils.isEmpty(message)) {
                view.toast("手术时间, 信息不全");
                return;
            } else if (i == 15 && TextUtils.isEmpty(message)) {
                view.toast("手术医院, 信息不全");
                return;
            } else if (i == 16 && TextUtils.isEmpty(message)) {
                view.toast("手术医生, 信息不全");
                return;
            }
        }

//        // 出生日期
//        final String s1 = mPatient[8];
//        final float f1 = Float.parseFloat(s1.substring(0, 4));
//        final float f2 = Float.parseFloat(s1.substring(5, 7));
//        final float f3 = Float.parseFloat(s1.substring(9, 11));
//        // 手术时间
//        final String s2 = mPatient[14];
//        final float f4 = Float.parseFloat(s2.substring(0, 4));
//        final float f5 = Float.parseFloat(s2.substring(5, 7));
//        final float f6 = Float.parseFloat(s2.substring(9, 11));
//        if (f4 < f1 || f5 < f2 || f6 < f3) {
//            view.toast("手术时间不能小于出生日期");
//            return;
//        }

        HashMap<String, Object> params2 = new HashMap<>();
        params2.put("real_name", mPatient[0]);
        params2.put("phone", mPatient[1]);
        params2.put("idcard", mPatient[2]);
        params2.put("height", mPatient[3]);
        params2.put("weight", mPatient[4]);
        params2.put("email", mPatient[5]);
        params2.put("supervisor_doctor", mPatient[6]);
        params2.put("sex", "男".equals(mPatient[7]) ? "1" : "0");
        params2.put("birthday", mPatient[8]);
        params2.put("address", mPatient[9]);
        final String[] split = mPatient[9].split(File.separator);
        final String provinceid = DBManager.getInstance().getAreaCode(split[0]);
        params2.put("provinceid", provinceid);
        final String cityid = DBManager.getInstance().getAreaCode(split[1]);
        params2.put("cityid", cityid);
        final String regionid = DBManager.getInstance().getAreaCode(split[2]);
        params2.put("regionid", regionid);
        params2.put("blood_type", mPatient[11]);

        HashMap<String, Object> params3 = new HashMap<>();
        params3.put("donor_receptor_type", mPatient[10]);
        params3.put("transplant_type", mPatient[12]);
        params3.put("source_type", mPatient[13]);
        params3.put("operation_date", mPatient[14]);
        params3.put("hospital_name", mPatient[15]);
        final String hospitalId = DBManager.getInstance().getHospitalId(mPatient[15]);
        params3.put("hospitalid", hospitalId);
        params3.put("doctor_name", mPatient[16]);

        HashMap<String, Object> params1 = new HashMap<>();
        params1.put("patient_info", params2);
        params1.put("operation_info", params3);

        final RequestBody params = createParams(params1, HttpParams.DATA_TYPE_1_4);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().compileInfo(params);

        request(observable, new OnModelAcceptChangeListener<Object>() {
            @Override
            public void modelSucc(Object result) {

                final User patient = DBManager.getInstance().syncGetUserModel();
                DBManager.getInstance().updataUserModel(patient);

                view.goMain();
            }
        });
    }

    void initAdapter(CompileView view, FragmentManager manager) {

        if (null == view || null == manager) return;

        CompileAdapter adapter = new CompileAdapter(manager);
        view.setCompilePager(adapter);
    }

    void sorllNext(CompileView view, ViewPager pager, TextView step2) {

        int index = pager.getCurrentItem();
        boolean isNull = ((CompileAdapter) pager.getAdapter()).isNull(index);
        if (isNull) {
            view.toast("信息未填写");
            return;
        }

        int position = (index + 1);
        if (position >= pager.getAdapter().getCount()) {
            compileInfo(view);
            return;
        }

        pager.setCurrentItem(position);
        if (position + 1 == pager.getAdapter().getCount()) {
            view.setText(step2, "提交");
        } else {
            view.setText(step2, "下一步");
        }
    }

    void sorllPreview(CompileView view, ViewPager pager, TextView step2) {

        int position = pager.getCurrentItem() - 1;
        if (position < 0)
            return;

        pager.setCurrentItem(position);
        view.setText(step2, "下一步");
    }

    @Override
    public void recycler() {
    }
}
